<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
global $logid;
//,$uid,$user;
$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();


//write to database
if(isset($_POST['action'])) {
	$action=trim($_POST['action']);
	if($action=='black_list') {
		//use loop to ensure single entrance and single exit
		do{
			$params = array();
			//$params['id'] = intval(trim($_POST['id']));
			$params['id'] = $logid;
			$params['blockusername'] = edp_db_input( $_POST['blockusername'] );

			//first check params
			if( edp_is_empty_string($params['id'] ) )
				$errmsg->add( 'black_list', '用户不合法！', 'error' );
			if( edp_is_empty_string( $params['blockusername'] ) )
				$errmsg->add( 'black_list', '添加的用户名不合法！', 'error' );
			if( !$userDao->IsNickExist( $params['blockusername'] ) )
				$errmsg->add( 'black_list', '该用户不存在', 'error' );

			//assemble black list
			$pageVars['blacklists'] = $userDao->GetUserBlackList( $params['id'] );
			$arr_blacklist = array();
			if( $pageVars['blacklists'] ){
				foreach( $pageVars['blacklists'] as $item ){
					$arr_blacklist[] = $item['id'];
				}
			}

			$blockid = $userDao->GetUserIDByName( $params['blockusername'] );
			if( in_array( $blockid , $arr_blacklist) ){
				$errmsg->add( 'black_list', '该用户已经在你的黑名单中了', 'error' );
			}
			//check if it is himself
			if( $blockid == GetLoginUserID() )
				$errmsg->add( 'black_list', '不能添加自己', 'error' );

			$pageVars = array_merge( $pageVars, $params );
			if( $errmsg->size('black_list') > 0 )
			{
				break;
			}

			$arr_blacklistnew = array_merge($arr_blacklist, array( $blockid ) );
			$str_blacklistids = implode(",", $arr_blacklistnew);
			if( !$userDao->UpdateBlackList(  $params['id'], $str_blacklistids ) ) {
				$errmsg->add( 'black_list', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'black_list', '更新成功', 'success' );
			}

			//get user basic_info from database again
			$pageVars['blacklists'] = $userDao->GetUserBlackList( intval( $params['id'] ));

			break;
		}while(1);
	}
	else{
		$errmsg->add( 'black_list', '未知操作', 'error' );
	}
}
//delete user in black list
else if( isset($_GET['blackid']) ){
//	$userInfo = getUserFromCookie();
//	if( $userInfo ){
		$blackid = intval($_GET['blackid']);
		$arr_blacklistrevert = array( $blackid );

		//assemble blacklist_id
		$blacklists = $userDao->GetUserBlackList( $logid);
		$arr_blacklist = array();
		if( $blacklists ){
			foreach( $blacklists as $item ){
				$arr_blacklist[] = $item['id'];
			}
		}

		$arr_blacklistnew = array_diff($arr_blacklist, $arr_blacklistrevert );
		$str_blacklistids = implode(",", $arr_blacklistnew);
		
		if( $userDao->UpdateBlackList( $logid, $str_blacklistids ) ){
			$errmsg->add( 'black_list', '更新成功', 'success' );
		}
		else{
			$errmsg->add( 'black_list', '更新失败', 'error' );
		}
//	}
//	else{
//		$errmsg->add( 'black_list', '登录用户信息不合法', 'error' );
//	}

	$pageVars['id'] = intval($logid);
	//get user basic_info from database again
	$pageVars['blacklists'] = $userDao->GetUserBlackList( $logid);
}
//read from database
else{
//	$userInfo = getUserFromCookie();
//	if( $userInfo ){
		//get user basic_info from database
		$pageVars['blacklists'] = $userDao->GetUserBlackList( $logid);
		$pageVars['id'] = $logid;
//	}
//	else{
//		$errmsg->add( 'black_list', '登录用户信息不合法', 'error' );
//	}
}
?>
